package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by csuarez on 20/01/14.
 */
public class BibliotecaApp {

    public static void main(String[] args) {
        Library library = createLibrary();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        Librarian librarian = new Librarian();
        InputHandler inputHandler = new InputHandler(bufferedReader, librarian);
        String userActivity = null;
        createsUsers(librarian);

        System.out.println("Welcome! The library is available");
        printBorrowables(library.getAvailableBooks());

        while(true) {
            System.out.println(inputHandler.mainMenu());
            userActivity = inputHandler.getInput();

            if(userActivity.equalsIgnoreCase("Check Out")) {
                inputHandler.logInPrompt();
                inputHandler.checkOutPrompt(library);
            } else if(userActivity.equalsIgnoreCase("Check In")) {
                inputHandler.logInPrompt();
                inputHandler.checkInPrompt(library);
            } else if(userActivity.equalsIgnoreCase("List Books")) {
                printBorrowables(library.getAvailableBooks());
            }else if(userActivity.equalsIgnoreCase("List Movies")) {
                printBorrowables(library.getAvailableMovies());
            }else if(userActivity.equalsIgnoreCase("See my information")){
                User user = librarian.getCurrentUser();
                System.out.println(user.getInformation());
            }else{
                System.out.println("Select a valid option!");
            }
        }
    }

    private static void printBorrowables(List<Borrowable> borrowables) {
        for (Borrowable borrowable : borrowables) {
                System.out.println(borrowable.getTitle());
        }
    }

    private static Library createLibrary() {
        List<Borrowable> borrowables = new ArrayList<Borrowable>();

        borrowables.add(new Book("Rayuela"));
        borrowables.add(new Book("El Quijote"));
        borrowables.add(new Movie ("Fight Club", "David Fincher", "1999", "8"));
        borrowables.add(new Movie("Amelie", "Jean-Pierre Jeunet", "2001", "9"));

        return new Library(borrowables);
    }

    public static void createsUsers(Librarian librarian) {
        User user = new User("Carla Suárez", "csuarez@gmail.com", "3810008", "csuarez", "******");
        librarian.addUser(user);
    }
}